//-----------------------------------------------------------------------------
//
// @file	Transform.cpp
// @brief	Transform file
// @author	Vinod Melapudi
// @date	20Feb2010
//
//-----------------------------------------------------------------------------

#include "Transform.h"

Transform::Transform( const Matrix4x4& m )
{
	for( int i=0; i<4; i++ )
		row[i] = m[i];
}

// Convert to GL matrix
void Transform::ToGLMatrix( float *gl_mat ) const
{
	for( int i=0; i<4; i++ )
		for( int j=0; j<4; j++ )
			gl_mat[i+4*j] = row[i][j];
}

// Load from GL matrix
void Transform::FromGLMatrix( float *gl_mat )
{
	for( int i=0; i<4; i++ )
		for( int j=0; j<4; j++ )
			row[i][j] = gl_mat[i+4*j];
}

// Set translation
void Transform::SetTranslation( const Vector3& t )
{
	row[0].w = t.x;
	row[1].w = t.y;
	row[2].w = t.z;
}

// Get translation from mat
Vector3 Transform::GetTranslation() const
{
	return Vector3( row[0].w, row[1].w, row[2].w );
}

// Set rotation matrix with angle and axis
void Transform::SetRotation( AXIS axis, float angle )
{
	float c = cos(angle);
	float s = sin(angle);

	for( int i=0; i<3; i++ )
		for( int j=0; j<3; j++ )
			row[i][j] = ( i == j ) ? 1 : 0;

	switch( axis )
	{
	case AXIS_X:
		row[1].y = c;	row[1].z = -s;
		row[2].y = s;	row[2].z = c;
		break;
	case AXIS_Y:
		row[0].x = c;	row[0].z = s;
		row[2].x = -s;	row[2].z = c;		
		break;
	case AXIS_Z:
		row[0].x = c;	row[0].y = -s;
		row[1].x = s;	row[1].y = c;
		break;
	};
}